<?php

/**
 * Add search log (call XSSearch::addSearchLog)
 */
class LogAddForm extends CFormModel
{
	public $word;
	public $wdf = 1;

	public function rules()
	{
		return array(
			array('word', 'required'),
			array('wdf', 'numerical', 'allowEmpty' => false),
		);
	}

	public function attributeLabels()
	{
		return array(
			'word' => '搜索词',
			'wdf' => '次数',
		);
	}

	/**
	 * @param XS $xs
	 */
	public function save($xs)
	{
		try {
			$xs->search->addSearchLog($this->word, $this->wdf);
			$xs->index->flushLogging();
			return true;
		} catch (XSException $e) {
			$this->addError('word', $e->getMessage());
			return false;
		}
	}

	/**
	 * @return the first error
	 */
	public function getFirstError()
	{
		foreach ($this->getErrors() as $attribute => $errors) {
			return $errors[0];
		}
		return null;
	}
}
